package com.telit.firecommand.service.impl;

import com.telit.common.constant.RoleCodeConstants;
import com.telit.common.dto.SysUserRolePageDto;
import com.telit.firecommand.dao.SysUserDAO;
import com.telit.firecommand.entity.SysUser;
import com.telit.firecommand.service.FireAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Create by Chencf 2022/08/30
 */
@Service
public class FireAdminServiceImpl implements FireAdminService {

    @Autowired
    private SysUserDAO sysUserDAO;

    @Override
    public List<SysUser> fireManagerList() {
        return sysUserDAO.selectByCondition(null, null, null, true,
                RoleCodeConstants.ROLE_CODE_FIRE_MANAGER_ID, null, null, Integer.MAX_VALUE, 0);
    }

    @Override
    public List<String> fireMangerMobileList() {
        List<SysUser> userList = fireManagerList();
        if(!CollectionUtils.isEmpty(userList)){
            List<String> mobileList = new ArrayList<>();
            userList.forEach(user->{
                if(StringUtils.hasText(user.getMobile())){
                    mobileList.add(user.getMobile());
                }
            });
            return mobileList;
        }
        return null;
    }

    @Override
    public List<String> fireMangerIdList() {
        List<SysUser> userList = fireManagerList();
        if(!CollectionUtils.isEmpty(userList)){
            List<String> list = new ArrayList<>();
            userList.forEach(user->{
                if(user.getId() !=null){
                    list.add(String.valueOf(user.getId()));
                }
            });
            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> fireMangerIdListByDistrictCode(String districtCode) {
        SysUserRolePageDto dto = new SysUserRolePageDto();
        dto.setRoleCode("FH_MANAGER");
        dto.setRegionCode(districtCode);
        List<SysUser> userList = fireManagerListByDistrictCode(dto);
        if (!CollectionUtils.isEmpty(userList)) {
            List<String> list = new ArrayList<>();
            userList.forEach(user -> {
                if (user.getId() != null) {
                    list.add(String.valueOf(user.getId()));
                }
            });
            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> fireMangerIdListByDistrictCodeLevel(String districtCode) {
        SysUserRolePageDto dto = new SysUserRolePageDto();
        dto.setRoleCode("FH_MANAGER");
        dto.setRegionCode(districtCode);
        dto.setNotLike(1);
        List<SysUser> userList = fireManagerListByDistrictCode(dto);
        if (!CollectionUtils.isEmpty(userList)) {
            List<String> list = new ArrayList<>();
            userList.forEach(user -> {
                if (user.getId() != null) {
                    list.add(String.valueOf(user.getId()));
                }
            });
            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> fireMangerMobileListByDistrictCode(String districtCode) {
        SysUserRolePageDto dto = new SysUserRolePageDto();
        dto.setRoleCode("FH_MANAGER");
        dto.setRegionCode(districtCode);
        List<SysUser> userList = fireManagerListByDistrictCode(dto);
        if (!CollectionUtils.isEmpty(userList)) {
            List<String> mobileList = new ArrayList<>();
            userList.forEach(user -> {
                if (StringUtils.hasText(user.getMobile())) {
                    mobileList.add(user.getMobile());
                }
            });
            return mobileList;
        }
        return null;
    }

    private List<SysUser> fireManagerListByDistrictCode(SysUserRolePageDto dto) {
        return sysUserDAO.selectByCondition(null, null, dto.getRegionCode(), true,
                RoleCodeConstants.ROLE_CODE_FIRE_MANAGER_ID, null, null, Integer.MAX_VALUE, 0);
    }

}
